package org.fyp.synchrona.communication;



import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLStreamException;

import org.apache.log4j.Logger;
import org.fyp.synchrona.core.impl.AttributeComparator;

import org.fyp.synchrona.core.impl.ServiceManager;

import org.fyp.synchrona.core.info.impl.Account;
import org.fyp.synchrona.core.info.impl.ICommonDataObject;
import org.fyp.synchrona.core.info.impl.Update;

import org.fyp.synchrona.util.DataQueue;
import org.fyp.synchrona.util.IDMMLData;
import org.fyp.synchrona.util.ITask;
import org.fyp.synchrona.util.IUpdate;
import org.fyp.synchrona.util.PacketIn;
import org.fyp.synchrona.util.Task;
import org.fyp.synchrona.xml.impl.DMMLParser;

public class RequestHandler extends Thread {
	
	private static RequestHandler reqHandler;
	
	private static boolean isRunning ;
	
	private static boolean isInterepted;
	
	static final Logger logger = Logger.getLogger(RequestHandler.class);
	
	private RequestHandler(){
		
	}
	
	public void run(){
		
		while(RequestHandler.getRequestHandler().isRunning){
			
				IDMMLData dmmlData = null;
			
				
			
				PacketIn inData = null;
				try {
					int size = DataQueue.getDataQueueObject().getQueueSize();
					System.out.println("Size of the Queue ::"+size);
					inData = DataQueue.getDataQueueObject().dequeue();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				String userName = inData.getUserName();
				String dmml = inData.getBody();
				//for(int i=0;i<100000;i++){
				logger.info(" [INFO][INDATA][USERNAME] ::"+userName);
				logger.info(" [INFO][INDATA][BODY] ::"+dmml);
				System.out.println(" [INFO][INDATA][USERNAME] ::"+userName);
				System.out.println(" [INFO][INDATA][BODY] ::"+dmml);
					
				//}
			    /*get the dmmlParser object*/
				DMMLParser dmmlParser = DMMLParser.getDMMLParser();
				/*get the common profile object creating class object*/
				//Profiler p = Profiler.getProfilerObject();
				AttributeComparator attributeComparator = AttributeComparator.getAttributeComparatorObject();
				/**/
				ServiceManager sm = ServiceManager.getServiceMangerObject();
				try {
					
					dmmlData = dmmlParser.parseXML(dmml);
					if(dmmlData==null){
						return;
					}
				
				} catch (XMLStreamException e) {
					
					e.printStackTrace();
					
				}
				Iterator i = dmmlData.getAccount().listIterator();
				Iterator i1 = dmmlData.getTask().listIterator();
				Account element = null;
				ITask element1 = null;
				System.out.println("\n");
				System.out.println("\n");
				logger.info("###########################Testing DMML Data Object#############################");
				System.out.println("\n");
				
				/**
				 * printing service type
				 */
				
				logger.info("+++++++++++++++++++++++++++Service Name ++++++++++++++++++++++++++++++++++++++++");
				logger.info(dmmlData.getServiceName());
				logger.info("+++++++++++++++++++++++++++Update Type++++++++++++++++++++++++++++++++++++++++++");
				logger.info(dmmlData.getUpdateType());
				System.out.println("\n");
				System.out.println("\n");
				
				/**
				 * printing update data
				 */
				System.out.println("_+_+_+_+_+_+_+_+_+_+_+_+_+_update data+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_");
				while(i.hasNext()) {
					System.out.println("\n");
					element = (Account) i.next(); 
					logger.info("+++++++++++++++++++++++++++service name+++++++++++++++++++++++++++++++++++++++++");
					logger.info(element.getServiceName()+"\n");
					logger.info("+++++++++++++++++++++++++++user name++++++++++++++++++++++++++++++++++++++++++++");
					logger.info(element.getUserName()+"\n");
					logger.info("+++++++++++++++++++++++++++password+++++++++++++++++++++++++++++++++++++++++++++");
					logger.info(element.getUserName()+"\n");
					System.out.println("\n");
				} 
				
				while(i1.hasNext()) {
					System.out.println("\n");
					element1 = (Task) i1.next();
					logger.info("+++++++++++++++++++++++++++tagname++++++++++++++++++++++++++++++++++++++++++++++");
					System.out.print(element1.getAttributeName()+"\n");
					logger.info("+++++++++++++++++++++++++++tag value++++++++++++++++++++++++++++++++++++++++++++");
					System.out.print(element1.getAttributeValue()+"\n");
					System.out.println("\n");
				} 
				logger.info("_+_+_+_+_+_+_+_+_+_+_+_+_+_update data_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+_+");
				System.out.println("\n");
				logger.info("---------------------------DMMLData OBJECT TESTING FINISED----------------------");
				System.out.println("\n");
				logger.info("###########################MODELING COMMON DATA OBJECT##########################");
				System.out.println("\n");
				System.out.println("\n");
				ICommonDataObject cdo = attributeComparator.createCommonObject(dmmlData.getTask());
			    IUpdate updateObj = new Update();
			    System.out.println("\n");
			    System.out.println("\n");
			    logger.info("---------------------------MODELING COMMONG DATA OBJECT FINISHED----------------");
			    System.out.println("\n");
			    System.out.println("\n");
			    updateObj.setServiceType(dmmlData.getServiceName());
			    updateObj.setCdo(cdo);
			    updateObj.setUpdateType(dmmlData.getUpdateType());
			    updateObj.setAccount(dmmlData.getAccount());
			   

			    logger.info("***************************TESTING UPDATE OBJECT WHICH IS TO BE SENT THROUGH API");
			    System.out.println("\n");
			    System.out.println("\n");
			    logger.info("testing common profile object user name::"+updateObj.getCdo().getUSER_NAME());
				System.out.println("\n");
				logger.info("testing common profile object birth day ::"+updateObj.getCdo().getBIRTH_DAY());
				System.out.println("\n");
				logger.info("testing common profile object birht month ::"+updateObj.getCdo().getBIRTH_MONTH());
				System.out.println("\n");
				logger.info("testing common profile object birth year ::"+updateObj.getCdo().getBIRTH_YEAR());
				System.out.println("\n");
				logger.info("testing common profile object city ::"+updateObj.getCdo().getCITY());
				System.out.println("\n");
				logger.info("testing common profile object country ::"+updateObj.getCdo().getCOUNTRY());
				System.out.println("\n");
				logger.info("testing common profile object first name ::"+updateObj.getCdo().getFIRST_NAME());
				System.out.println("\n");
				logger.info("testing common profile object last name ::"+updateObj.getCdo().getLAST_NAME());
				System.out.println("\n");
				logger.info("testing common profile object full name ::"+updateObj.getCdo().getFULL_NAME());
				System.out.println("\n");
				logger.info("testing common profile object username ::"+updateObj.getCdo().getUSER_NAME());
				System.out.println("\n");
				logger.info("testing common profile object sex ::"+updateObj.getCdo().getSEX());
				System.out.println("\n");
				logger.info("testing common profile object get state ::"+updateObj.getCdo().getSTATE());
				System.out.println("\n");
				logger.info("testing common profile object username ::"+updateObj.getCdo().getUSER_NAME());
				System.out.println("\n");
				logger.info("testing common profile object address ::"+updateObj.getCdo().getADDRESS());
				System.out.println("\n");
				logger.info("testing common profile object about me ::"+updateObj.getCdo().getBIO());
				System.out.println("\n");
				logger.info("testing common profile object courntry city ::"+updateObj.getCdo().getCOUNTRY_CITY());
				System.out.println("\n");
				logger.info("testing common profile object current country city::"+updateObj.getCdo().getCURRENT_COUNTRY_CITY());
				System.out.println("\n");
				logger.info("testing common profile object home phone::"+updateObj.getCdo().getHOME_PHONE());
				System.out.println("\n");
				logger.info("testing common profile object mobile ::"+updateObj.getCdo().getMOBLIE());
				System.out.println("\n");
				logger.info("testing common profile object photo height ::"+updateObj.getCdo().getPHOTO_HEIGHT());
				System.out.println("\n");
				logger.info("testing common profile object photo url ::"+updateObj.getCdo().getPHOTO_URL());
				System.out.println("\n");
				logger.info("testing common profile object photo width ::"+updateObj.getCdo().getPHOTO_WIDTH());
				System.out.println("\n");
				logger.info("testing common profile object website ::"+updateObj.getCdo().getWEBSITE());
				System.out.println("\n");
				logger.info("testing common profile object STATUS ::"+updateObj.getCdo().getSTATUS());
				System.out.println("\n");
				logger.info("testing commong profile object EMAIL ::"+updateObj.getCdo().getEMAIL());
				System.out.println("\n");
				logger.info("testing commong profile object EMAIL ::"+updateObj.getCdo().getINTERESTS());
				sm.updateServiceProfiles(updateObj);
		}
		
	}
	
	/*initialization of request handler*/
	public static synchronized RequestHandler getRequestHandler(){
		if(reqHandler == null){
			reqHandler = new RequestHandler();
			return reqHandler;
		}
		return reqHandler;
	}
	
	/*return running status*/
	public static boolean returnRunningStatus(){
		return RequestHandler.isRunning;
	}
	
	/*set running status*/
	public static void setRunningStatus(boolean status){
		RequestHandler.isRunning = status;
		
	}
	
	/*this method is used to get the interrpt status*/
	public static boolean getInterruptStatus(){
		return RequestHandler.isInterepted;
	}
	
	/*this method is used to set the interrupt status*/
	public static  void setInterruptStatus(boolean interuptStatus){
		RequestHandler.isInterepted = interuptStatus;
	}
	/*this method is uesed to interrupt this thered*/
	public static final void interruptThread() {
		RequestHandler.getRequestHandler().isRunning = false;
		RequestHandler.isInterepted=true;
		RequestHandler.getRequestHandler().suspend();
		
		
	}
	
	public static final void restartThread(){
		
		RequestHandler.getRequestHandler().resume();
		RequestHandler.getRequestHandler().isRunning = true;
		RequestHandler.isInterepted=false;
		
	}
	
	
	
	

}
